/*----------------------------------------------------------
 * FILE: Oracle10gRowSetDynaClass.java
 * LOGICAL NAME: Oracle10gRowSetDynaClass
 * PRODUCT: jsiit-utils
 *----------------------------------------------------------
 * IMPORTANT NOTICE
 * This program is property of DAI ( Development Alternatives Inc )
 * Its unauthorized use, as any code alteration without authorization 
 * is prohibited
 *----------------------------------------------------------
 * MODIFICATIONS
 * DATE 27/09/2007
 * AUTHOR rrodriguez
 *----------------------------------------------------------
 */

package sv.gob.mh.dgii.jdbc;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.RowSetDynaClass;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @author TPAR <b>Created by </b>rrodriguez <b>for</b>jsiit-utils <b>on</b>
 *         27/09/2007 04:01:45 PM <b>Purpose</b>
 *         <p>
 *         </p>
 */

@SuppressWarnings("unchecked")
public class Oracle10gRowSetDynaClass extends RowSetDynaClass {


	/**
	 * 
	 */
	private static final long serialVersionUID = -6402058401656362020L;
	
	Log log = LogFactory.getLog(Oracle10gRowSetDynaClass.class);
	
	

	public Oracle10gRowSetDynaClass(ResultSet rs) throws SQLException {
		super(rs);
	}
	
	
	@Override
	protected DynaProperty createDynaProperty(ResultSetMetaData metadata, int i) throws SQLException {
		String columnName = metadata.getColumnName(i);
        //String name = lowerCase ? columnName.toLowerCase() : columnName;
		String name = columnName;		
		

        String className = null;
        try {
            int sqlType = metadata.getColumnType(i);
            switch (sqlType) {
                case java.sql.Types.DATE:
                    return new DynaProperty(name, java.sql.Date.class);
                case java.sql.Types.TIMESTAMP:
                    return new DynaProperty(name, java.sql.Timestamp.class);
                case java.sql.Types.TIME:
                    return new DynaProperty(name, java.sql.Time.class);
                default:
                    className = metadata.getColumnClassName(i);
            }
        } catch (SQLException e) {
            // this is a patch for HsqlDb to ignore exceptions
            // thrown by its metadata implementation
        }

        // Default to Object type if no class name could be retrieved
        // from the metadata
        Class clazz = Object.class;
        if (className != null) {
            clazz = loadClass(className);
        }
        DynaProperty dp = new DynaProperty(name, clazz);        
        return dp;
	}

	
	protected void copy(ResultSet resultSet) throws SQLException {
		while (resultSet.next()) {
			DynaBean bean = new BasicDynaBean(this);
			for (int i = 0; i < properties.length; i++) {
				String name = properties[i].getName();
				Object value = null;
				if (properties[i].getType().equals(Timestamp.class)) {
					value = resultSet.getTimestamp(name);
				} else {
					value = resultSet.getObject(name);
				}// if timestamp
				bean.set(name, value);
			}// for
			rows.add(bean);
		}// while
	}

}
